package com.itstherules.jmsunit.expectation.xml;

import org.junit.Test;

import com.itstherules.jmsunit.expectation.xml.comparison.XmlIdenticalExpectation;
import com.itstherules.jmsunit.expectation.xml.comparison.XmlSimilarExpectation;


public class XmlEqualsExpectationTest {

	@Test
	public void equalsTag() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation("<root/>");
		unit.compute("<root/>");
	}
	
	@Test(expected=AssertionError.class)
	public void errorDueToNotEqualsTag() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation("<root/>");
		unit.compute("<shoot/>");
	}
	
	@Test
	public void equalsValue() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation("<root>root_value</root>");
		unit.compute("<root>root_value</root>");
	}
	
	@Test(expected=AssertionError.class)
	public void errorDueToNotEqualsValue() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation("<root>root</root>");
		unit.compute("<root>shoot</root>");
	}

	@Test
	public void equalsEncodingIgnored() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><root/>");
		unit.compute("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>");
	}

	@Test
	public void equalsWithMoreNodes() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation("<fishing_list><fish>halibut</fish><fish><name>mackrel</name><kind>oily</kind></fish><fish>cod</fish></fishing_list>");
		unit.compute("<fishing_list><fish>halibut</fish><fish><name>mackrel</name><kind>oily</kind></fish><fish>cod</fish></fishing_list>");
	}


	@Test
	public void equalsWithMoreNodesAndDifferentWhitespace() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation(
				"<fishing_list>" +
				"	<fish>halibut</fish>" +
				"	<fish>" +
				"		<name>mackrel</name>" +
				"		<kind>oily</kind>" +
				"	</fish>" +
				"	<fish>cod</fish>" +
				"</fishing_list>"
		);
		
		unit.compute(
				"<fishing_list>" +
				"	     <fish>halibut</fish>\n" +
				"			<fish>\n" +
				"				<name>mackrel</name>\n" +
				"				<kind>oily</kind>\n" +
				"			</fish>\n" +
				"	<fish>cod</fish>\n" +
				"</fishing_list>"				
		);
	}

	@Test(expected=AssertionError.class)
	public void errorDueToIncorrectNodes() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation(
				"<fish_list>" +
				"	<fish>flatfish</fish>" +
				"	<crustacean>" +
				"		<crab>land</crab>" +
				"		<crab>land</crab>" +
				"		<shell>scallop</shell>" +
				"	</crustacean>" +
				"</fish_list>");
		
		unit.compute(
				"<fish_list>" +
				"	<fish>flatfish</fish>" +
				"	<pet>koi</pet>" +
				"	<crustacean>" +
				"		<crab>land</crab>" +
				"		<crab>land</crab>" +
				"	</crustacean>" +
			"</fish_list>");
	}

	@Test
	public void equalsNamespace() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation("<root:shoot xmlns:root=\"http://localhost/root/shoot\">root_value</root:shoot>");
		unit.compute("<root:shoot xmlns:root=\"http://localhost/root/shoot\">root_value</root:shoot>");
	}
	
	@Test
	public void similarAfterRedefiningNamespace() throws Exception {
		XmlSimilarExpectation unit = new XmlSimilarExpectation("<root:root xmlns:root=\"http://localhost/root\">root</root:root>");
		unit.compute("<shoot:root xmlns:shoot=\"http://localhost/root\">root</shoot:root>");
	}

	
	@Test(expected=AssertionError.class)
	public void errorDueToDifferentNamespace() throws Exception {
		XmlIdenticalExpectation unit = new XmlIdenticalExpectation(
				"<fishing_list:fishing_list xmlns:fishing_list=\"http://localhost/fishing_list\">fishing_list</fishing_list:fishing_list>");
		unit.compute("<fishing_list:fishing_list xmlns:fishing_list=\"http://localhost/squishy_list\">fishing_list</fishing_list:fishing_list>");
	}
	
	@Test
	public void equalsMoreNodesAndNamespace() throws Exception {
		XmlSimilarExpectation unit = new XmlSimilarExpectation(
				"<?xml version=\"1.0\" ?>\n" +
				"<fishing_list:fishy xmlns:fishing_list=\"http://localhost/fishing_list\">\n" +
				"	<fish>Clown</fish>\n" +
				"	<eyes:amount xmlns:eyes=\"http://localhost/amount\">four</eyes:amount>\n" +
				"	<head>bulbous</head>" +
				"	<body>chunky</body>\n" +
				"</fishing_list:fishy>");
		unit.compute(
				"<wishing_list:fishy xmlns:wishing_list=\"http://localhost/fishing_list\" xmlns:fins=\"http://localhost/amount\">" +
				"	<fish>Clown</fish>" +
				"	<fins:amount>four</fins:amount>" +
				"	<head>bulbous</head>" +
				"	<body>chunky</body>" +
				"</wishing_list:fishy>");
	}

}
